import productItemRow from '@/components/product-item/components/product-item-row.vue'

import { fetchContent , fetchGuess } from '@/api/index.js';

export default {
	components: {
		productItemRow
	},
	data() {
		return {
			titleNViewBackground: 'rgb(203, 87, 60)',
			titleNViewBackgroundList: ['rgb(203, 87, 60)', 'rgb(205, 215, 218)'],
			swiperCurrent: 0,
			swiperLength: 0,
			advertiseList: [],
			categoryList: [],
			shopList: [],
			newProductList: [],
			hotProductList: [],
			guessProductList: [],
			pageParam: {
				pageNum: 0,
				pageSize: 5
			},
			loadingType:'more',
		}
	},
	onLoad() {
		this.loadData();
	},
	//下拉刷新
	onPullDownRefresh(){
		this.pageParam.pageNum=0;
		this.guessProductList = [];
		this.loadData();
	},
	//加载更多
	onReachBottom(){
		//如果没有更多，直接跳过不执行
		if(this.loadingType == 'nomore'){
			return;
		}
		//下一页
		this.pageParam.pageNum++;
		this.loadingType = 'loading';
		//加载猜你喜欢
		this.loadGuess();
	},
	methods: {
		/**
		 * 加载数据
		 */
		async loadData() {
			fetchContent().then(response => {
				let data = response.data;
				this.advertiseList = data.advertiseList;
				this.swiperLength = this.advertiseList.length;
				this.categoryList = data.categoryList;
 				this.shopList = data.shopList;
				this.newProductList = data.newProductList;
				this.hotProductList = data.hotProductList;
				//加载猜你喜欢
				this.loadGuess();
			});
		},
		
		/**
		 * 加载猜你喜欢数据
		 */
		async loadGuess() {
			
			fetchGuess(this.pageParam).then(response => {
				let pageData = response.data;
				//在原先list中追加数据
				this.guessProductList = this.guessProductList.concat(pageData.content);
				
				if(pageData.last){
					//没有更多了
					this.loadingType = 'nomore';
				}else{
					this.loadingType = 'more';
				}
				
				uni.stopPullDownRefresh();
			})
		},
		
		
		//--------------------------------------------------------------------------------------------------
		//<!-- 头部轮播  开始  -->
		//轮播图切换修改背景色
		swiperChange(e) {
			const index = e.detail.current;
			this.swiperCurrent = index;
			let changeIndex = index % this.titleNViewBackgroundList.length;
			this.titleNViewBackground = this.titleNViewBackgroundList[changeIndex];
		},
		//广告详情页
		navToAdvertisePage(item) {
			let id = item.id;
			console.log("navToAdvertisePage",item)
		},
		//<!-- 头部轮播  结束  -->
		//--------------------------------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------------------------------
		//<!-- 精选分类  开始  -->
		navToCategoryList(id,name) {
			uni.navigateTo({
				url: `/pages/category/list/list?categoryId=${id}&categoryName=${name}`
			})
		},
		//<!-- 精选分类  结束  -->
		//--------------------------------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------------------------------
		//<!-- 品牌制造商直供  开始  -->
		navToShopListPage() {
			uni.navigateTo({
				url: `/pages/shop/list/list`
			})
		},
		//详情
		navToShopDetailPage(item) {
			uni.navigateTo({
				url: `/pages/shop/shop?id=${item.id}`
			})
		},
		//<!-- 品牌制造商直供  结束  -->
		//--------------------------------------------------------------------------------------------------

		//--------------------------------------------------------------------------------------------------
		//<!-- 新鲜好物  开始  -->
		navToNewProudctListPage() {
			uni.navigateTo({
				url: `/pages/index/new/new`
			})
		},
		//--------------------------------------------------------------------------------------------------
		//<!-- 新鲜好物  结束  -->
		
		//--------------------------------------------------------------------------------------------------
		//<!-- 人气推荐  开始  -->
		navToHotProudctListPage() {
			uni.navigateTo({
				url: `/pages/index/hot/hot`
			})
		},
		//--------------------------------------------------------------------------------------------------
		//<!-- 人气推荐  结束  -->
		
		//详情
		navToDetailPage(item) {
			uni.navigateTo({
				url: `/pages/product/product?sku=${item.sku}`
			})
		},
	}
}